<template>
  <div class="select-company-dialog-container">
    <el-dialog
      v-model="dialogVisible"
      :before-close="handleClose"
      :title="title"
      width="40%"
    >
      <el-form label-position="left" label-width="80px">
        <el-form-item label="公司：">
          <el-select v-model="companyId" placeholder="请选择">
            <el-option
              v-for="item in list"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue'
  import { GetCompany } from '@/api/mupro'
  const props = defineProps({
    visible: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      default: '选择公司',
    },
  })
  const dialogVisible = ref(false)
  watch(
    () => props.visible,
    () => {
      dialogVisible.value = props.visible
    }
  )
  const list: any = ref([])
  const companyId: any = ref('')
  const getCompanyList = async () => {
    const { data } = await GetCompany({
      pageNo: 0,
      pageSize: 0,
      total: 0,
      tableName: 'string',
      sort: 'string',
      order: 'string',
      wheres: 'string',
      export: true,
      value: 'string',
    })
    list.value = data?.rows.map((item: any) => {
      return {
        value: item.CompanyId,
        label: item.FullName,
      }
    })
  }
  getCompanyList()
  const emit = defineEmits(['close', 'confirm'])
  const handleClose = () => {
    dialogVisible.value = false
    emit('close', dialogVisible.value)
  }
  const $baseMessage: any = inject('$baseMessage')
  const confirm = () => {
    if (companyId.value === '') {
      $baseMessage('请选择公司！', 'warning', 'vab-hey-message-warning')
      return
    }
    emit('confirm', companyId.value)
    // dialogVisible.value = false
  }
</script>

<style lang="scss" scoped></style>
